package com.gusha.app;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gusha.dao.ReceivingAddressDao;
import com.gusha.dao.SysAssetsDao;
import com.gusha.dao.SysFriendsDao;
import com.gusha.dao.SysGroundDao;
import com.gusha.dao.SysMailingDao;
import com.gusha.dao.SysOrderDao;
import com.gusha.dao.SysRepositoryDao;
import com.gusha.dao.SysShoppingDao;
import com.gusha.dao.UserDao;
import com.gusha.dao.UserMoneyDao;
import com.gusha.model.ReceivingAddress;
import com.gusha.model.SysAssets;
import com.gusha.model.SysFriends;
import com.gusha.model.SysGround;
import com.gusha.model.SysMailing;
import com.gusha.model.SysOrder;
import com.gusha.model.SysShopping;
import com.gusha.model.User;
import com.gusha.model.UserMoney;
import com.gusha.service.ReceivingAddressService;
import com.gusha.service.SysAssetsService;
import com.gusha.service.SysFriendsService;
import com.gusha.service.SysShoppingService;
import com.gusha.service.SysWarehouseService;
import com.gusha.service.UserService;
import com.gusha.utils.Constants;
import com.gusha.utils.JsonResult;
import com.gusha.utils.UpdateUtil;
import com.gusha.utils.WebValueUtil;


@CrossOrigin(origins="*",maxAge=3600)
@Controller
@RequestMapping("/app/share")
public class AppShareController {

	@Autowired
	private SysShoppingDao sysShoppingDao;
	@Autowired
	private SysShoppingService sysShoppingService;
	@Autowired
	private SysOrderDao sysOrderDao;
	@Autowired
	private ReceivingAddressDao receivingAddressDao;
	@Autowired
	private ReceivingAddressService receivingAddressService;
	@Autowired
	private SysFriendsService sysFriendsService;
	@Autowired
	private SysFriendsDao sysFriendsDao;
	@Autowired
	private SysRepositoryDao sysRepositoryDao;
	@Autowired
	private UserService userService;
	@Autowired
	private UserDao userDao;
	@Autowired
	private SysGroundDao sysGroundDao;
	@Autowired
	private SysWarehouseService sysWarehouseService;
	@Autowired
	private SysAssetsDao sysAssetsDao;
	@Autowired
	private SysMailingDao sysMailingDao;
	@Autowired
	private SysAssetsService sysAssetsService;
	@Autowired
	private UserMoneyDao userMoneyDao;
	
	@CrossOrigin(origins="*",maxAge=3600)
	@RequestMapping("/toGroundTrue")
	@ResponseBody
	public JsonResult toGroundTrue(Model model,HttpServletRequest request) {
		JsonResult result=new JsonResult(false);
		String type = WebValueUtil.getWebValue(request, "type");
		String user = WebValueUtil.getWebValue(request, "user");
		if(StringUtils.isNotBlank(user)&&StringUtils.isNotBlank(type)) {
			User uid=new User();
			uid.setId(new BigInteger(user));
			List<SysGround> list=sysGroundDao.findAllByUserIdAndGroundType(uid,new BigInteger(type));
			if(list.size()>0) {
				result.setMsg("操作成功");
				result.setFlag(true);
				return result;
			}else {
				result.setMsg("操作成功");
				return result;
			}
		}
		
		return result;
	}

	//商城列表
	@CrossOrigin(origins="*",maxAge=3600)
	@RequestMapping("/toShoppingList")
	@ResponseBody
	public JsonResult toShoppingList(Model model,HttpServletRequest request) {
		JsonResult result=new JsonResult(false);
		String pageIndex = WebValueUtil.getWebValue(request, "pageIndex");
		String type = WebValueUtil.getWebValue(request, "type");
		int pageNo = 0;
		if (StringUtils.isNotBlank(pageIndex)) {
			pageNo = Integer.parseInt(pageIndex);
		}
		SysShopping sysShopping=new SysShopping();
		if(StringUtils.isNotBlank(type)) {
			sysShopping.setCommodityType(new BigInteger(type));
		}
		if(StringUtils.isNotBlank(type)) {
			sysShopping.setIsDelete(new BigInteger("0"));
		}
		Sort sort=new Sort(Sort.Direction.DESC, "addTime");
		Pageable pageable = PageRequest.of(pageNo, Constants.DEFAULT_PAGESIZE, sort);
		Page<SysShopping> page=sysShoppingService.findAll(pageable, sysShopping);

		result.setMsg("操作成功");
		result.setObj(page);
		result.setFlag(true);
		return result;

	}
	//共享商城订单
	@CrossOrigin(origins="*",maxAge=3600)
	@PostMapping("/toOrder")
	@ResponseBody
	public JsonResult toOrder(Model model,HttpServletRequest request) {
		JsonResult result=new JsonResult(false);
		String str1 = WebValueUtil.getWebValue(request, "str1");
		System.out.println(str1);
		JSONArray jsa=JSONArray.parseArray(str1);

		User user=new User();
		for (int i = 0; i<jsa.size(); i++) {
			JSONObject js=(JSONObject) jsa.get(i);
			//JSONObject currentdata=JSONObject.parseObject(jsa.get(i).toString());
		
			
			System.out.println(js);
			//JSONObject jso=JSONObject.parseObject(js);
			String id=js.getString("id");
			String number=js.getString("number");
			String userId=js.getString("user");
			System.out.println(id+"---------"+number+"---------"+userId);
			SysOrder sysOrder=new SysOrder();
			SysShopping sysShopping=new SysShopping();
			sysShopping.setId(new BigInteger(id));
			sysOrder.setShoppingId(sysShopping);
			sysOrder.setCommodityNum(new BigInteger(number));
			Date date=new Date();
			sysOrder.setAddTime(date);

			user.setId(new BigInteger(userId));
			sysOrder.setUserId(user);
			sysOrder.setIsDelete(new BigInteger("0"));
			sysOrder.setState(new BigInteger("1"));
			ReceivingAddress address=new ReceivingAddress();
			address.setId(new BigInteger("0"));
			sysOrder.setAddressId(address);
			String unm=sysOrder.getCommodityNum().toString();
			int num=Integer.parseInt(unm);
			if(num>0) {//产品数量大于0加入数据库
			sysOrderDao.saveAndFlush(sysOrder);
			}
		}

		//List<SysOrder> list=sysOrderDao.findAllByUserIdAndState(user,new BigInteger("1"));
		// List<ReceivingAddress> addressList=receivingAddressDao.findAllByUser(user);
		// Map<String,Object> map=new HashMap<>();
		//map.put("list", list);
		//map.put("addressList",addressList);

		//result.setObj(list);
		result.setMsg("订单生成完成");
		result.setFlag(true);
		return result;
	}
	//进入订单列表
	@CrossOrigin(origins="*",maxAge=3600)
	@PostMapping("/toOrderList")
	@ResponseBody
	public JsonResult toOrderList(Model model,HttpServletRequest request) {
		JsonResult result=new JsonResult(false);
		String userId = WebValueUtil.getWebValue(request, "user");
		if(StringUtils.isNotBlank(userId)) {
			User user= new User();
			user.setId(new BigInteger(userId));
		List<SysOrder> list=sysOrderDao.findAllByUserIdAndState(user,new BigInteger("1"));
		ReceivingAddress address=null;
		if(list.get(0).getAddressId()!=null) {
		SysOrder sysOrder=list.get(0);
		 address=sysOrder.getAddressId();
		}
		Long zong=0L;
		for (SysOrder sysOrder2 : list) {
			BigDecimal price=sysOrder2.getShoppingId().getCommodityPrice();
			String price1=price.toString();
			Long price2=Long.valueOf(price1);
			String strnum=sysOrder2.getCommodityNum().toString();
			Long num=Long.valueOf(strnum);
			zong+=price2 * num;
		}
		Map<String,Object> map=new HashMap<>();
		map.put("orderList", list);
		map.put("address", address);
		map.put("allPrice", zong);
		result.setMsg("查询成功");
		result.setObj(map);
		}
		return result;
		}
	//删除订单接口
	@CrossOrigin(origins="*",maxAge=3600)
	@RequestMapping("/toOrderDelete")
	@ResponseBody
	public JsonResult toOrderDelete(Model model,HttpServletRequest request) {
		JsonResult result=new JsonResult(false);
		String id = WebValueUtil.getWebValue(request, "id");
		if(StringUtils.isNotBlank(id)) {
			if(sysOrderDao.existsById(new BigInteger(id))) {
		sysOrderDao.deleteById(new BigInteger(id));
		result.setMsg("删除成功");
		result.setFlag(true);
			}
		}
		return result;
	}
	//添加地址
	@CrossOrigin(origins="*",maxAge=3600)
	@RequestMapping("/toSaveAddress")
	@ResponseBody
	public JsonResult toSaveAddress(ReceivingAddress receivingAddress,HttpServletRequest request) {

		JsonResult result=new JsonResult(false,"操作失败");
		Date date=new Date();
		receivingAddress.setAddTime(date);
		receivingAddress.setIsDelete(new BigInteger("0"));
		receivingAddressDao.saveAndFlush(receivingAddress);

		result.setMsg("添加成功");
		result.setFlag(true);
		return result;

	}
	//地址列表
	@CrossOrigin(origins="*",maxAge=3600)
	@RequestMapping("/toAddressList")
	@ResponseBody
	public JsonResult toAddressList(Model model,HttpServletRequest request) {
		JsonResult result=new JsonResult(false,"操作失败");

		String pageIndex = WebValueUtil.getWebValue(request, "pageIndex");
		String user = WebValueUtil.getWebValue(request, "user");
		int pageNo = 0;
		if (StringUtils.isNotBlank(pageIndex)) {
			pageNo = Integer.parseInt(pageIndex);
		}

		ReceivingAddress receivingAddress=new ReceivingAddress();
		if (StringUtils.isNotBlank(user)) {
			User u=new User();
			u.setId(new BigInteger(user));
			receivingAddress.setUser(u);
			receivingAddress.setIsDelete(new BigInteger("0"));
		}
		Sort sort=new Sort(Sort.Direction.DESC, "addTime");
		Pageable pageable = PageRequest.of(pageNo, Constants.DEFAULT_PAGESIZE, sort);
		Page<ReceivingAddress> page=receivingAddressService.findAll(pageable, receivingAddress);

		result.setMsg("操作成功");
		result.setObj(page);
		result.setFlag(true);
		return result;
	}

	//用户选择地址
	@CrossOrigin(origins="*",maxAge=3600)
	@RequestMapping("/toUserSet")
	@ResponseBody
	public JsonResult toUserSet(Model model,HttpServletRequest request) {
		JsonResult result=new JsonResult(false,"操作失败");
		String address = WebValueUtil.getWebValue(request, "address");//地址id
		String user = WebValueUtil.getWebValue(request, "user");//用户id
		User u=new User();
		u.setId(new BigInteger(user));
		List<SysOrder> list=sysOrderDao.findAllByUserId(u);
		ReceivingAddress receivingAddress=new ReceivingAddress();
		receivingAddress.setId(new BigInteger(address));
		for (SysOrder sysOrder : list) {
			SysOrder s=new SysOrder();
			UpdateUtil.copyNonNullProperties(s, sysOrder);
			s.setAddressId(receivingAddress);
			sysOrderDao.saveAndFlush(s);

		}

		result.setMsg("设置成功");
		result.setFlag(true);
		return result;
	}
	//修改收货地址
	@CrossOrigin(origins="*",maxAge=3600)
	@RequestMapping("/toUpdateAddress")
	@ResponseBody
	public JsonResult toUpdateAddress(Model model, HttpServletRequest request) {
		JsonResult result=new JsonResult(false,"操作失败");
		String id = WebValueUtil.getWebValue(request, "id");
		if (StringUtils.isNotBlank(id)) {
			if(receivingAddressDao.existsById(new BigInteger(id))) {
				ReceivingAddress receivingAddress=receivingAddressDao.findAllById(new BigInteger(id));
				result.setMsg("修改成功");
				result.setObj(receivingAddress);
				result.setFlag(true);
				return result;
			}
		}  
		return result;
	}

	//删除收货地址
	@CrossOrigin(origins="*",maxAge=3600)
	@RequestMapping("/toDeleteAddress")
	@ResponseBody
	public JsonResult toDeleteAddress(Model model, HttpServletRequest request) {
		JsonResult result=new JsonResult(false,"操作失败");
		String id = WebValueUtil.getWebValue(request, "id");
		if(StringUtils.isNotBlank(id)) {
			ReceivingAddress receivingAddress=receivingAddressDao.findAllById(new BigInteger(id));
			receivingAddress.setIsDelete(new BigInteger("1"));
			receivingAddressDao.saveAndFlush(receivingAddress);
			result.setMsg("删除成功");
			result.setFlag(true);
		}
		return result;

	}
	// 支付成功 & 添加资产
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping("/toPaySuccess")
	@ResponseBody
	public JsonResult toPaySuccess(Model model, HttpServletRequest request) {
		JsonResult result = new JsonResult(false, "操作失败");
		String user = WebValueUtil.getWebValue(request, "user");
		User u = new User();

		u.setId(new BigInteger(user));
		List<SysOrder> list = sysOrderDao.findAllByUserIdAndState(u, new BigInteger("1"));
		for (SysOrder sysOrder : list) {

			sysOrder.setState(new BigInteger("2"));
			sysOrderDao.saveAndFlush(sysOrder);

			// 添加资产
			SysAssets sysAssets = new SysAssets();
			sysAssets.setUserId(u);
			sysAssets.setShoppingType(sysOrder.getShoppingId().getCommodityType()); // 添加商品类型
			sysAssets.setShoppingName(sysOrder.getShoppingId().getCommodityName()); // 添加商品名字
			sysAssets.setShoppingImg(sysOrder.getShoppingId().getCommodityImg()); // 添加商品图片
			sysAssets.setShoppingNum(sysOrder.getCommodityNum().longValue()); // 添加购买的商品数量
			sysAssets.setPayTime(sysOrder.getAddTime()); // 添加购买商品时间
			sysAssets.setStealNum(0L); // 偷来的数量为 0
			sysAssets.setPlantNum(0L); // 偷来的数量为 0
			sysAssets.setFinishedNum(0L); // 偷来的数量为 0
			sysAssetsDao.saveAndFlush(sysAssets);

		}

		result.setMsg("订单完成");
		result.setFlag(true);
		// result.setObj(s);
		return result;
	}



	// 好友
	@CrossOrigin(origins="*",maxAge=3600)
	@PostMapping("/toFriendsList")
	@ResponseBody
	public JsonResult toFriendsList(Model model,HttpServletRequest request) {
		JsonResult result=new JsonResult(false);
		String pageIndex = WebValueUtil.getWebValue(request, "pageIndex");
		String user = WebValueUtil.getWebValue(request, "user");
		int pageNo = 0;
		if (StringUtils.isNotBlank(pageIndex)) {
			pageNo = Integer.parseInt(pageIndex);
		}
		SysFriends sysFriends=new SysFriends();
		if (StringUtils.isNotBlank(user)) {
			User u=new User();
			u.setId(new BigInteger("user"));
			sysFriends.setUser(u);
		}
		Sort sort=new Sort(Sort.Direction.DESC, "addTime");
		Pageable pageable = PageRequest.of(pageNo, Constants.DEFAULT_PAGESIZE, sort);
		Page<SysFriends> page=sysFriendsService.findAll(pageable, sysFriends);

		result.setMsg("操作成功");
		result.setObj(page);
		result.setFlag(true);
		return result;

	}


	//搜索用户
	@CrossOrigin(origins="*",maxAge=3600)
	@PostMapping("/toSosoUser")
	@ResponseBody
	public JsonResult toSosoUser(Model model,HttpServletRequest request) {
		JsonResult result=new JsonResult(false);

		String str = WebValueUtil.getWebValue(request, "str");
		String str1=str;
		List<User> list=userDao.findAllByUserNameOrPhone(str,str1);
		result.setMsg("操作成功");
		result.setObj(list);
		result.setFlag(true);
		return result;
	}
	//发起添加
	@CrossOrigin(origins="*",maxAge=3600)
	@PostMapping("/toAddFriends")
	@ResponseBody
	public JsonResult toAddFriends(Model model,HttpServletRequest request) {
		JsonResult result=new JsonResult(false);
		String user = WebValueUtil.getWebValue(request, "user");
		String friend = WebValueUtil.getWebValue(request, "friend");
		if(StringUtils.isNotBlank(friend)&&StringUtils.isNotBlank(user)) {
			SysFriends friends=new SysFriends();
			User u=new User();
			u.setId(new BigInteger(user));
			friends.setUser(u);//设置用户id
			
			User f=new User();
			f.setId(new BigInteger(friend));
			friends.setFriend(f);//设置好友id
			
			Date date=new Date();
			friends.setAddTime(date);//设置添加时间
			friends.setState(new BigInteger("0"));//发起状态
			sysFriendsDao.saveAndFlush(friends);
			result.setMsg("发起成功");
			result.setFlag(true);
			
		}
		return result;
	}

	//好友提示列表
	@CrossOrigin(origins="*",maxAge=3600)
	@PostMapping("/toMessage")
	@ResponseBody
	public JsonResult toMessage(Model model,HttpServletRequest request) {
		JsonResult result=new JsonResult(false);
		String userId = WebValueUtil.getWebValue(request, "userId");
		User user=new User();
		user.setId(new BigInteger(userId));
		List<SysFriends> list=sysFriendsDao.findAllByFriend(user);
		result.setMsg("操作成功");
		result.setObj(list);
		result.setFlag(true);
		return result;
	}
	//好友提示详情
	@CrossOrigin(origins="*",maxAge=3600)
	@PostMapping("/toMessageDetails")
	@ResponseBody
	public JsonResult toMessageDetails(Model model,HttpServletRequest request) {
		JsonResult result=new JsonResult(false);
		String id = WebValueUtil.getWebValue(request, "id");
		if(StringUtils.isNotBlank(id)) {
			SysFriends friend=sysFriendsDao.findAllById(new BigInteger(id));
			result.setObj(friend);
			result.setMsg("操作成功");
			result.setFlag(true);
		}

		return result;
	}
	//验证好友
	@CrossOrigin(origins="*",maxAge=3600)
	@PostMapping("/toProving")
	@ResponseBody
	public JsonResult toProving(SysFriends friend,HttpServletRequest request) {
		JsonResult result=new JsonResult(false);
		sysFriendsDao.saveAndFlush(friend);//修改验证状态并按照id重新保存
		result.setMsg("操作成功");
		result.setFlag(true);
		//}
		return result;
	}

	//购买土地
	@CrossOrigin(origins="*",maxAge=3600)
	@RequestMapping("/toAddGround")
	@ResponseBody
	public JsonResult toAddGround(SysGround sysGround,HttpServletRequest request) {
		JsonResult result=new JsonResult(false);
		sysGround.setPaymentState(new BigInteger("0"));
		sysGroundDao.saveAndFlush(sysGround);
		result.setMsg("操作成功");
		result.setFlag(true);
		return result;
	}
	
	@CrossOrigin(origins="*",maxAge=3600)
	@RequestMapping("/toGroundOrder")
	@ResponseBody
	public JsonResult toGroundOrder(Model model,HttpServletRequest request) {
		JsonResult result=new JsonResult(false);
		String user = WebValueUtil.getWebValue(request, "user");

		if(StringUtils.isNotBlank(user)) {
			User uid=new User();
			uid.setId(new BigInteger(user));

			List<SysGround> list=sysGroundDao.findByUserIdAndPaymentState(uid,new BigInteger("0"));
			result.setObj(list);
			result.setMsg("操作成功");
			result.setFlag(true);
			return result;
		}

		return result;
	}
	@CrossOrigin(origins="*",maxAge=3600)
	@RequestMapping("/toGroundOrderDelete")
	@ResponseBody
	public JsonResult toGroundOrderDelete(Model model,HttpServletRequest request) {
		JsonResult result=new JsonResult(false);
		String id = WebValueUtil.getWebValue(request, "id");
		if(StringUtils.isNotBlank(id)) {
		sysGroundDao.deleteById(new BigInteger(id));
	
			result.setMsg("操作成功");
			result.setFlag(true);
			return result;
		}

		return result;
	}
	//土地支付完成
	@CrossOrigin(origins="*",maxAge=3600)
	@RequestMapping("/toGroundPay")
	@ResponseBody
	public JsonResult toGroundPay(Model model,HttpServletRequest request) {
		JsonResult result=new JsonResult(false);
		String id = WebValueUtil.getWebValue(request, "id");
		if(StringUtils.isNotBlank(id)) {
			SysGround sysGround= sysGroundDao.getOne(new BigInteger(id));
			sysGround.setPaymentState(new BigInteger("1"));//设置支付状态
			Date date=new Date();
			sysGround.setPaymentTime(date);
			sysGroundDao.saveAndFlush(sysGround);
			result.setMsg("操作成功");
			result.setFlag(true);
			return result;
		}
		return result;
	}
	@CrossOrigin(origins="*",maxAge=3600)
	@RequestMapping("/groundAddress")
	@ResponseBody
	public JsonResult groundAddress(Model model,HttpServletRequest request) {
		JsonResult result=new JsonResult(false);
		String id = WebValueUtil.getWebValue(request, "id");
		if(StringUtils.isNotBlank(id)) {
			if(receivingAddressDao.existsById(new BigInteger(id))) {
				ReceivingAddress address=receivingAddressDao.findAllById(new BigInteger(id));
				result.setObj(address);
				result.setMsg("操作成功");
				result.setFlag(true);
			}
		}
		return result;
	}
	//其他农场列表
	@CrossOrigin(origins="*",maxAge=3600)
	@RequestMapping("/toOtherHouse")
	@ResponseBody
	public JsonResult toOtherHouse(Model model,HttpServletRequest request) {
		JsonResult result=new JsonResult(false);
		String pageIndex = WebValueUtil.getWebValue(request, "pageIndex");
		int pageNo = 0;
		if (StringUtils.isNotBlank(pageIndex)) {
			pageNo = Integer.parseInt(pageIndex);
		}

		SysAssets sysAssets=new SysAssets();
		Sort sort=new Sort(Sort.Direction.DESC, "shoppingNum");//按照余额排行
		Pageable pageable = PageRequest.of(pageNo, Constants.DEFAULT_PAGESIZE, sort);
		Page<SysAssets> page=sysAssetsService.findAll(pageable, sysAssets);
		result.setObj(page);
		result.setMsg("操作成功");
		result.setFlag(true);
		return result;
	}
	

	
	//进入我的资产
	@CrossOrigin(origins="*",maxAge=3600)
	@RequestMapping("/toMyAssets")
	@ResponseBody
	public JsonResult toMyAssets(Model model,HttpServletRequest request) {
		JsonResult result=new JsonResult(false);
		String userId = WebValueUtil.getWebValue(request, "userId");
		if(StringUtils.isNotBlank(userId)) {
			User user=new User();
			user.setId(new BigInteger(userId));
			
			Long num1=0L;//蔬菜
			Long num2=0L;//水产
			Long num3=0L;//家禽
			Long num4=0L;//水果
			Long num5=0L;//花卉
			List<SysAssets> list=sysAssetsDao.findAllByUserId(user);
			
			for (SysAssets sysAssets : list) {
				if(sysAssets.getShoppingType().toString().equals("1")) {
					num1+=sysAssets.getShoppingNum()+sysAssets.getStealNum()+sysAssets.getFinishedNum()+sysAssets.getPlantNum();
				}
				if(sysAssets.getShoppingType().toString().equals("2")) {
					num2+=sysAssets.getShoppingNum()+sysAssets.getStealNum()+sysAssets.getFinishedNum()+sysAssets.getPlantNum();
				}
				if(sysAssets.getShoppingType().toString().equals("3")) {
					num3+=sysAssets.getShoppingNum()+sysAssets.getStealNum()+sysAssets.getFinishedNum()+sysAssets.getPlantNum();
				}
				if(sysAssets.getShoppingType().toString().equals("4")) {
					num4+=sysAssets.getShoppingNum()+sysAssets.getStealNum()+sysAssets.getFinishedNum()+sysAssets.getPlantNum();
				}
				if(sysAssets.getShoppingType().toString().equals("5")) {
					num5+=sysAssets.getShoppingNum()+sysAssets.getStealNum()+sysAssets.getFinishedNum()+sysAssets.getPlantNum();
				}
			}
			
			Map<String,Long> map=new HashMap<>();
			map.put("shucai", num1);//蔬菜数量
			map.put("shuichan", num2);//水产数量
			map.put("jiaqin", num3);//家禽数量
			map.put("shuiguo", num4);//水果数量
			map.put("huahui", num5);//花卉数量
			
			result.setMsg("操作成功");
			result.setObj(map);
			result.setFlag(true);
			return result;
		}

		return result;
	}
	@CrossOrigin(origins="*",maxAge=3600)
	@RequestMapping("/toUserMoney")
	@ResponseBody
	public JsonResult toUserMoney(Model model,HttpServletRequest request) {
		JsonResult result=new JsonResult(false);
		String user = WebValueUtil.getWebValue(request, "user");
		if(StringUtils.isNotBlank(user)) {
			User u=new User();
			u.setId(new BigInteger(user));
			UserMoney money=userMoneyDao.findAllByUser(u);
			BigDecimal userM=money.getMoney();
			
			result.setObj(userM);
			result.setMsg("操作成功");
			result.setFlag(true);
			return result;
		}
		
		return result;
	}
	//仓库列表  前端需传 1蔬菜水果花卉类  2家禽水产类  3所有剩余种子
	@CrossOrigin(origins="*",maxAge=3600)
	@RequestMapping("/toMyAassetsList")
	@ResponseBody
	public JsonResult toMyAassetsList(Model model,HttpServletRequest request) {
		JsonResult result=new JsonResult(false);
		String userId = WebValueUtil.getWebValue(request, "userId");
		String type = WebValueUtil.getWebValue(request, "type");
		if(StringUtils.isNotBlank(userId)&&StringUtils.isNotBlank(type)) {
			User user=new User();
			user.setId(new BigInteger(userId));
		if(type.equals("1")) {
			
			List<SysAssets> list=sysAssetsDao.findAllByShoppingTypeAndUserId(new BigInteger(type),user);
			result.setObj(list);
			result.setMsg("操作成功");
			result.setFlag(true);
			return result;
		}
		if(type.equals("2")) {
			
			List<SysAssets> list=sysAssetsDao.findAllByUserIdAndShoppingType(user,new BigInteger(type));
			result.setObj(list);
			result.setMsg("操作成功");
			result.setFlag(true);
			return result;
		}
		if(type.equals("3")) {
			
			List<SysAssets> list=sysAssetsDao.findAllByUser(user);
			result.setObj(list);
			result.setMsg("操作成功");
			result.setFlag(true);
			return result;
		}
		}
		return result;
	}
	
	// 邮递仓库产品
		@CrossOrigin(origins = "*", maxAge = 3600)
		@RequestMapping("/toMailing")
		@ResponseBody
		public JsonResult toMailing(Model model, HttpServletRequest request) {
			JsonResult result = new JsonResult(false, "操作失败");
			// str="[{'assetsId':'7','finishedNum':'5','userId':'17','receivingAddress':'8'},{'id':'8',"number":"5",'userId':'17','receivingAddress':'9'}]";
			String str = WebValueUtil.getWebValue(request, "str");
			JSONArray jsa = JSONArray.parseArray(str);
			SysAssets assets = new SysAssets();
			User user = new User();
			ReceivingAddress receivingAddress1 = new ReceivingAddress();
			int num = 0;
			for (int i = 0; i < jsa.size(); i++) {
				JSONObject js = (JSONObject) jsa.get(i);
				System.out.println(js);
				// JSONObject jso=JSONObject.parseObject(js);
				String assetsId = js.getString("assetsId");// 资产id
				String finishedNum = js.getString("finishedNum");// 邮递成品数量
				String userId = js.getString("userId");// 用户id
				String receivingAddress = js.getString("receivingAddress");// 地址id

				assets.setId(new BigInteger(assetsId));// 资产ID
				user.setId(new BigInteger(userId));
				SysAssets list = sysAssetsDao.findAllById(new BigInteger(assetsId));// 根据资产ID查询资产信息
			
					if (list.getFinishedNum() >= Long.valueOf(finishedNum)) {
						list.setFinishedNum(list.getFinishedNum() - Long.valueOf(finishedNum));// 该ID资产中剩余成品数量
						sysAssetsDao.saveAndFlush(list);
						
					 // 往邮递表插入邮递信息数据
						receivingAddress1.setId(new BigInteger(receivingAddress));
						SysMailing sysMailing = new SysMailing();
						sysMailing.setUserId(user); // 设 邮递用户ID
						sysMailing.setAssetsId(assets);// 设 邮递资产ID
						sysMailing.setReceivingAddress(receivingAddress1); // 设 邮递收获地址
						sysMailing.setProductNum(new BigInteger(finishedNum));// 设 邮递数量
						sysMailing.setState(new BigInteger("1")); // 未 邮递
						sysMailing.setIsDelete(new BigInteger("0"));// 未 邮递 逻辑 未删除
						Date date = new Date();
						sysMailing.setAddTime(date); // 设 邮递时间
						sysMailingDao.saveAndFlush(sysMailing);
						num += sysMailing.getProductNum().intValue();// 总数量
						result.setMsg("正在发送邮递");
					}else {
						result.setMsg("超过产品余量");
						result.setFlag(false);
						return result;
					}
				
			}
			Map<String,Object> map=new HashMap<>();
			List<SysMailing> list2 = sysMailingDao.findAllByAssetsId(assets);// 根据资产ID查询邮递信息
			map.put("list", list2);
			map.put("num",num);

			result.setObj(map);
			result.setFlag(true);
			return result;
		}
		
		//进入种植页面
		@CrossOrigin(origins = "*", maxAge = 3600)
		@RequestMapping("/toPlantPage")
		@ResponseBody
		public JsonResult toPlantPage(Model model, HttpServletRequest request) {
			JsonResult result = new JsonResult(false, "操作失败");
			String user = WebValueUtil.getWebValue(request, "user");
			String type = WebValueUtil.getWebValue(request, "type");
			if(StringUtils.isNotBlank(type)&&StringUtils.isNotBlank(user)) {
				User uid=new User();
				uid.setId(new BigInteger(user));
				List<SysAssets> list=sysAssetsDao.findAllByUserAndShoppingType(uid,new BigInteger(type));
				result.setObj(list);
				result.setMsg("操作成功");
				result.setFlag(true);
			}
			
			return result;
		}
		//种植
		@CrossOrigin(origins = "*", maxAge = 3600)
		@RequestMapping("/toPlant")
		@ResponseBody
		public JsonResult toPlant(Model model, HttpServletRequest request) {
			JsonResult result = new JsonResult(false, "操作失败");
			String str = WebValueUtil.getWebValue(request, "str");
			//String type = WebValueUtil.getWebValue(request, "type");
			//str=[{"id":"17","plantNum":"5"},{"id":"17","plantNum":"5"}]
			JSONArray jsa=JSONArray.parseArray(str);
		
			for (int i = 0; i < jsa.size(); i++) {
				JSONObject js=(JSONObject) jsa.get(i);
				String id=js.getString("id");
				String number=js.getString("plantNum");
				
				SysAssets sysAssets=sysAssetsDao.findAllById(new BigInteger(id));
				sysAssets.setShoppingNum(sysAssets.getShoppingNum()-Long.valueOf(number));
				sysAssets.setPlantNum(sysAssets.getPlantNum()+Long.valueOf(number));
				sysAssetsDao.saveAndFlush(sysAssets);
				
			}
			result.setMsg("操作成功");
			result.setFlag(true);
			return result;
		}
		
		

}
